<template>
  <div class="dashboard-editor-container">

    <panel-group :average-amt="averageAmt" :today-amt="todayAmt"/>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData"/>
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart />
        </div>
      </el-col>
    </el-row>

    <el-row :gutter="8">
      <el-col :xs="{span: 24}" :sm="{span: 24}" :md="{span: 24}" :lg="{span: 12}" :xl="{span: 12}" style="padding-right:8px;margin-bottom:30px;">
        <transaction-table :top-phone-amt="topPhoneAmt" />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <todo-list />
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
        <box-card />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from './components/PanelGroup'
import LineChart from './components/LineChart'
import RaddarChart from './components/RaddarChart'
import PieChart from './components/PieChart'
import BarChart from './components/BarChart'
import TransactionTable from './components/TransactionTable'
import TodoList from './components/TodoList'
import BoxCard from './components/BoxCard'

export default {
  name: 'DashboardAdmin',
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    TransactionTable,
    TodoList,
    BoxCard
  },
  data () {
    return {
      lineChartData: {},
      averageAmt: 0,
      todayAmt: 0,
      topPhoneAmt: []
    }
  },
  created () {
    this.fetchLineChartData()
    this.fetchAmt()
    // this.todayAmt = amt.todayAmt
    // this.averageAmt = amt.averageAmt
    // this.topPhoneAmt = amt.topPhoneAmt
    // console.log(amt.topPhoneAmt)
  },
  methods: {
    // 每日工作量曲线
    fetchLineChartData () {
      let self = this
      this.$pool.query('select FROM_UNIXTIME(substr(create_time, 1, 10),"%Y-%m-%d %W.") day,count(1) amt from record group by FROM_UNIXTIME(substr(create_time, 1, 10),"%Y-%m-%d %W.")', function (err, rows) {
        if (err) {
        }
        let data = JSON.parse(JSON.stringify(rows))
        let lineChartData = {
          xData: [],
          yData: []
        }
        data.sort((a, b) => {
          return new Date(a.day.split(' ')[0]) - new Date(b.day.split(' ')[0])
        })
        data.forEach(item => {
          lineChartData.xData.push(item.day)
          lineChartData.yData.push(item.amt)
        })
        self.lineChartData = lineChartData
      })
    },
    fetchAmt () {
      // 工作总量
      let self = this
      this.$pool.query('select count(1) amt from record', function (err, rows) {
        if (err) {
        }
        let totolAmt = rows[0].amt
        self.$pool.query('select count(1) amt from (select count(1) from record group by FROM_UNIXTIME(substr(create_time, 1, 10),"%Y-%m-%d")) a', function (err, rows) {
          if (err) {
          }
          let days = rows[0].amt
          // 日均工作量
          self.averageAmt = totolAmt / days
        })
      })
      const startTimeStamp = new Date(new Date().toLocaleDateString()).getTime()
      this.$pool.query('select count(1) amt from record where create_time>? order by create_time desc', [startTimeStamp], function (err, rows) {
        if (err) {
          console.log(err)
        }
        self.todayAmt = rows[0].amt
      })
      // 按电话号码（部门）统计
      // let topPhoneAmt = this.$db.get('record')
      //   .countBy('phone')
      //   .toPairs()
      //   .sortBy(n => { return -n[1] })
      //   .take(10)
      //   .value()
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
